package com.caunock.sqlbuilder.adapter.group;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.caunock.sqlbuilder.adapter.AbstractDDLAdapter;
import com.caunock.sqlbuilder.meta.Column;
import com.caunock.sqlbuilder.meta.Index;
import com.caunock.sqlbuilder.meta.Table;

import java.util.List;

/**
 * 高标准SQL遵循组
 * 组员: Oracle, PostgreSQL, DB2, GreatDB, 达梦DM, 人大金仓KingBase
 *
 * @author Qinweitong
 */
public abstract class StandardGroupDDLAdapter extends AbstractDDLAdapter {

    @Override
    public String listDatabases() {
        return "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA";
    }

    @Override
    public String listTables(String schema) {
        return "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA  = '" + schema + "'";
    }

    @Override
    public StringBuilder comment(StringBuilder builder, Table meta) {
        // 直接返回，不做任何处理，使用commentOn的方式进行处理
        return builder;
    }

    @Override
    public StringBuilder comment(StringBuilder builder, Column<?> column) {
        // 直接返回，不做任何处理，使用commentOn的方式进行处理
        return builder;
    }


    @Override
    public StringBuilder increment(StringBuilder builder, Column<?> meta) {
        return builder;
    }

    @Override
    public StringBuilder tableCommentOn(StringBuilder builder, Table meta) {
        String tableComment = meta.getComment();
        if (StrUtil.isEmpty(tableComment)) {
            return builder;
        }
        builder.append(BR).append("COMMENT ON TABLE ")
                .append(meta.getName())
                .append(" IS ")
                .append(NAME_QUOTE)
                .append(tableComment)
                .append(NAME_QUOTE)
                .append(";");
        return builder;
    }

    @Override
    public StringBuilder columnCommentOn(StringBuilder builder, Table meta) {
        List<Column<?>> columns = meta.getColumns();
        if (CollUtil.isEmpty(columns)) {
            return builder;
        }
        for (Column<?> column : columns) {
            String columnName = column.getName();
            String columnComment = column.getComment();
            if (StrUtil.isBlank(columnComment)) {
                continue;
            }
            builder.append(BR).append("COMMENT ON COLUMN ")
                    .append(meta.getName())
                    .append(DELIMITER)
                    .append(columnName)
                    .append(" IS ")
                    .append(NAME_QUOTE)
                    .append(columnComment)
                    .append(NAME_QUOTE)
                    .append(";");
        }
        return builder;
    }

    @Override
    public StringBuilder indexCommentOn(StringBuilder builder, Index meta) {
        String comment = meta.getComment();
        if (StrUtil.isEmpty(comment)) {
            return builder;
        }
        builder.append(BR)
                .append("COMMENT ON INDEX ")
                .append(meta.getName())
                .append(" IS ")
                .append(NAME_QUOTE)
                .append(comment)
                .append(NAME_QUOTE)
                .append(";")
        ;
        return builder;
    }
}
